package com.bridgeintelligent.tag.webserver.dim.mgmt.service.export.excel;

import com.bridgeintelligent.tag.webserver.dim.mgmt.api.BaseCode;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.DimException;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.DimPage;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.DimRep;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.export.DimAggData;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.export.DimCompareData;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.export.ExcelCatalogModel;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.export.ExcelReaderCheckRsp;
import com.wayneleo.quickstart.framework.BaseResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author JackCheung
 * @date 2020/7/6 3:44 PM.
 * description:
 */
@Api(tags = "批量导入标签")
@RestController
@RequestMapping("/api/dimExcel")
@AllArgsConstructor
@Validated
@Slf4j
public class DimExcelExportController {

    private IDimExcelReaderService iDimExcelReaderService;
    private IDimExcelMgmtService iDimExcelMgmtService;

    @ApiOperation(value = "导入excel", notes = "标签excel导入")
    @PostMapping("/doExport")
    public DimRep upload(@RequestParam("update") MultipartFile excelFile) {
        try {
            ExcelReaderCheckRsp checkRsp = iDimExcelReaderService.upload(excelFile);
            return new DimRep<>(checkRsp, BaseCode.SUCCESS);
        } catch (DimException e) {
            log.error("标签excel导入出现异常：", e);
            return new DimRep<>(null,e.getCode(),e.getMessage());
        } catch (Exception e) {
            log.error("标签excel导入出现异常：", e);
            return new DimRep<>( null, 999, e.getMessage() );
        }
    }

    @ApiOperation(value = "下载上次上传的excel", notes = "标签excel导入")
    @GetMapping("/downLoad")
    public void downLoad(HttpServletResponse response, HttpServletRequest request) {
        iDimExcelMgmtService.downLoad(response,request);
    }

    @ApiOperation(value = "确认上传", notes = "标签excel导入")
    @GetMapping("/confirm")
    public BaseResponse confirm() {
        iDimExcelMgmtService.confirm();
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setCode(BaseCode.SUCCESS);
        return baseResponse;
    }

    @ApiOperation(value = "根据类型全部上传", notes = "标签excel导入")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "type", value = "类型 0 新增 1 覆盖")})
    @GetMapping("/replaceByType")
    public BaseResponse replaceByType(@NotBlank String type) {
        iDimExcelMgmtService.replaceByType(type);
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setCode(BaseCode.SUCCESS);
        return baseResponse;
    }

    @ApiOperation(value = "获取上传的标签列表", notes = "标签excel导入")
    @ApiImplicitParams(
                {@ApiImplicitParam(paramType = "query", name = "pageNumber", value = "页码"),
                 @ApiImplicitParam(paramType = "query", name = "pageSize", value = "条数"),
                 @ApiImplicitParam(paramType = "query", name = "type", value = "0为新增、1为更新"),
                 @ApiImplicitParam(paramType = "query", name = "dimName", value = "标签名称"),
                 @ApiImplicitParam(paramType = "query", name = "classify", value = "标签类型"),
                 @ApiImplicitParam(paramType = "query", name = "startTime", value = "开始时间"),
                 @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间"),
                 @ApiImplicitParam(paramType = "query", name = "catalogId", value = "目录字符串")
                })
    @GetMapping("/list")
    public DimRep list(Integer pageNumber, Integer pageSize, String type,String dimName,String classify,String startTime,String endTime,String catalogId) {
        pageNumber = pageNumber == null ? 1 : pageNumber;
        pageSize = pageSize == null ? 10 : pageSize;
        DimPage<DimAggData> dataDimPage = iDimExcelMgmtService.list(pageNumber, pageSize, type,dimName,classify,startTime,endTime,catalogId);
        return new DimRep<>(dataDimPage, BaseCode.SUCCESS);
    }

    @ApiOperation(value = "上传标签详情", notes = "标签excel导入")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "dimId", value = "标签ID")})
    @GetMapping("/detail")
    public DimRep confirm(String dimId) throws IllegalAccessException {
        DimCompareData dimCompareData = iDimExcelMgmtService.detail(dimId);
        return new DimRep<>(dimCompareData, BaseCode.SUCCESS);
    }

    @ApiOperation(value = "批量替换/全选替换", notes = "标签excel导入")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "dimIds", value = "标签ID,多个以逗号隔开")})
    @PostMapping("/batchReplace")
    public DimRep batchReplace(String dimIds) {
        dimIds = dimIds.replace("\"", "");
        iDimExcelMgmtService.batchReplace(dimIds.split(","));
        return new DimRep<>(BaseCode.SUCCESS);
    }

    @ApiOperation(value = "全部替换", notes = "标签excel导入")
    @PostMapping("/replaceAll")
    public DimRep replaceAll() {
        iDimExcelMgmtService.replaceAll();
        return new DimRep<>(BaseCode.SUCCESS);
    }

    @ApiOperation(value = "查询所有二级目录", notes = "查询所有二级目录")
    @PostMapping("/secondCatalog")
    public DimRep secondCatalog() {
        List<ExcelCatalogModel> list = iDimExcelMgmtService.findSecondCatalog();
        return new DimRep<>(list, BaseCode.SUCCESS);
    }
}
